home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / modprolg / mod-prol.lha / Prolog / Examples / examples.mod < prev    next >
Text File  |  1992-05-21  |  14KB  |  612 lines

  1. % ---------------------------------------------------------------------------
  2.  
  3. % File : EXAMPLES.MOD
  4. % Author : Brian Paxton
  5. % Date : 9/12/1991
  6.  
  7. % THIS IS A FILE CONTAINING EXAMPLES OF MODULE CONSTRUCTS. EACH IS GIVEN
  8. % A MINI DESCRIPTION OF WHAT IT IS TRYING TO DO. ANY DESCRIPTION 
  9. % BEGINNING WITH 'WARNING' OR 'ERROR', ARE DESIGNED TO TEST THE ROBUSTNESS OF
  10. % ERROR HANDLING.
  11.  
  12. % ---------------------------------------------------------------------------
  13.  
  14. % Testing invalid signature name (I)
  15.  
  16. % $consult(user,[v,s]).
  17. signature $test =
  18.   sig
  19.     pred test/1.
  20.   end.
  21.  
  22. % ---------------------------------------------------------------------------
  23.  
  24. % Testing invalid signature name (II)
  25.  
  26. % $consult(user,[v,s]).
  27. signature test(test) =
  28.   sig
  29.     pred test/1.
  30.   end.
  31.  
  32. % ---------------------------------------------------------------------------
  33.  
  34. % Testing invalid structure name (I)
  35.  
  36. % $consult(user,[v,s]).
  37. structure $test =
  38.   struct
  39.     pred test/1.
  40.   end.
  41.  
  42. % ---------------------------------------------------------------------------
  43.  
  44. % Testing invalid structure name (II)
  45.  
  46. % $consult(user,[v,s]).
  47. structure test(test) =
  48.   struct
  49.     pred test/1.
  50.   end.
  51.  
  52. % ---------------------------------------------------------------------------
  53.  
  54. % Testing invalid structure name (II)
  55.  
  56. % $consult(user,[v,s]).
  57. functor $test(x) =
  58.   struct
  59.     pred test/1.
  60.   end.
  61.  
  62. % ---------------------------------------------------------------------------
  63.  
  64. % Testing for duplicate structures in structure spec.
  65.  
  66. % $consult(user,[v,s]).
  67. signature test =
  68.   sig
  69.     pred test/1.
  70.   end.
  71. signature btreesig =
  72.   sig
  73.     pred isleaf/1 and isnode/1 and mkleaf/1 and mknode/4 and
  74.               label/2 and leftchild/2 and rightchild/2.
  75.     structure yah/test and yah/test.
  76.   end.
  77.  
  78. % ---------------------------------------------------------------------------
  79.  
  80. % Testing an invalid signature match.
  81.  
  82. % $consult(user,[v,s]).
  83. signature btreesig =
  84.   sig
  85.     pred isleaf/1 and isnode/1 and mkleaf/1 and mknode/4 and
  86.               label/2 and leftchild/2 and rightchild/2.
  87.   end.
  88.  
  89. structure btree/btreesig =
  90.   struct
  91.     fun leaf/0 and tree/3.
  92.     isleaf(leaf).
  93.   end.
  94.  
  95. % ---------------------------------------------------------------------------
  96.  
  97. % Ambiguous functions.
  98.  
  99. % $consult(user,[v,s]).
  100. structure yah =
  101.   struct
  102.     fun b/0 and b/1.
  103.     fun c = b.
  104.   end.
  105.  
  106. % ---------------------------------------------------------------------------
  107.  
  108. % Testing for structure spec when structure already defined.
  109.  
  110. % $consult(user,[v,s]).
  111. signature btreesig =
  112.   sig
  113.     pred isleaf/1.
  114.   end.
  115.  
  116. signature test =
  117.   sig
  118.     structure btree/btreesig.
  119.     structure btree/btreesig.
  120.   end.
  121.  
  122. % ---------------------------------------------------------------------------
  123.  
  124. % Testing for unknown structure in structure declaration.
  125.  
  126. % $consult(user,[v,s]).
  127. structure test = test2.
  128.  
  129. % ---------------------------------------------------------------------------
  130.  
  131. % Testing application of unknown functor.
  132.  
  133. % $consult(user,[v,s]).
  134. structure test = test2(a,b).
  135.  
  136. % ---------------------------------------------------------------------------
  137.  
  138. % Testing for unknown pred/fun during translation.
  139.  
  140. % $consult(user,[v,s]).
  141. structure test =
  142.   struct
  143.     test(one).
  144.   end.
  145.  
  146. % ---------------------------------------------------------------------------
  147.  
  148. % Testing for unknown structure in structure declaration 
  149. % (second type).
  150.  
  151. % $consult(user,[v,s]).
  152. structure test = test2:yah.
  153.  
  154. % ---------------------------------------------------------------------------
  155.  
  156. % Testing unknown signature in structure declaration.
  157.  
  158. % $consult(user,[v,s]).
  159. structure btree/btreesig =
  160.   struct
  161.     fun leaf/0 and tree/3.
  162.     isleaf(leaf).
  163.   end.
  164.  
  165. % ---------------------------------------------------------------------------
  166.  
  167. % Testing functor application with wrong number of arguments.
  168.  
  169. % $consult(user,[v,s]).
  170. signature btreedatasig1 =
  171.   sig
  172.     pred isleaf/1 and isnode/1 and mkleaf/1 and mknode/4 and
  173.               label/2 and leftchild/2 and rightchild/2.
  174.   end.
  175.  
  176. structure btreedata0/btreedatasig1 =
  177.   struct
  178.     fun leaf/0 and tree/3.
  179.     isleaf(leaf).
  180.     isnode(tree(_,_,_)).
  181.     mkleaf(leaf).
  182.     mknode(A,L,R,tree(A,L,R)).
  183.     label(tree(A,_,_),A).
  184.     leftchild(tree(_,L,_),L).
  185.     rightchild(tree(_,_,R),R).
  186.   end.
  187.  
  188. signature abstreememsig =
  189.   sig
  190.     structure b/btreedatasig1.
  191.     pred newmember/2.
  192.   end.
  193.  
  194. functor absbtreemem(x/btreedatasig1)/abstreememsig =
  195.   struct
  196.     structure b = x.
  197.     newmember(A,Tree) :-
  198.     b:label(Tree,A).
  199.     newmember(A,Tree) :-
  200.     b:leftchild(Tree,Left),
  201.     newmember(A,Left).
  202.     newmember(A,Tree) :-
  203.     b:rightchild(Tree,Right),
  204.     newmember(B,Right).
  205.   end.
  206.  
  207. structure testtree = absbtreemem(btreedata0,yah).
  208.  
  209. % ---------------------------------------------------------------------------
  210.  
  211. % Testing functor application.
  212.  
  213. % $consult(user,[v,s]).
  214. signature btreedatasig1 =
  215.   sig
  216.     pred isleaf/1 and isnode/1 and mkleaf/1 and mknode/4 and
  217.               label/2 and leftchild/2 and rightchild/2.
  218.   end.
  219.  
  220. structure btreedata0/btreedatasig1 =
  221.   struct
  222.     fun leaf/0 and tree/3.
  223.     isleaf(leaf).
  224.     isnode(tree(_,_,_)).
  225.     mkleaf(leaf).
  226.     mknode(A,L,R,tree(A,L,R)).
  227.     label(tree(A,_,_),A).
  228.     leftchild(tree(_,L,_),L).
  229.     rightchild(tree(_,_,R),R).
  230.   end.
  231.  
  232. signature abstreememsig =
  233.   sig
  234.     structure b/btreedatasig1.
  235.     pred newmember/2.
  236.   end.
  237.  
  238. functor absbtreemem(x/btreedatasig1)/abstreememsig =
  239.   struct
  240.     structure b = x.
  241.     newmember(A,Tree) :-
  242.     b:label(Tree,A).
  243.     newmember(A,Tree) :-
  244.     b:leftchild(Tree,Left),
  245.     newmember(A,Left).
  246.     newmember(A,Tree) :-
  247.     b:rightchild(Tree,Right),
  248.     newmember(B,Right).
  249.   end.
  250.  
  251. structure testtree = absbtreemem(btreedata0).
  252.  
  253. % Examples
  254.  
  255. % testtree:b:isnode(X).
  256. % (testtree:b:mknode(1,2,3,Node),testtree:b:isnode(Node)).
  257. % btreedata0:isleaf(X).
  258. % (testtree:b:mknode(1,2,3,Node),testtree:b:leftchild(Node,X),write(X),nl).
  259.  
  260. % ---------------------------------------------------------------------------
  261.  
  262. % Testing bad functions (I).
  263.  
  264. % $consult(user,[v,s]).
  265. structure one =
  266.   struct
  267.     fun a/1.
  268.   end.
  269.  
  270. structure two =
  271.   struct
  272.     fun b/1 = a.
  273.   end.
  274.  
  275. % ---------------------------------------------------------------------------
  276.  
  277. % Testing bad functions (II).
  278.  
  279. % $consult(user,[v,s]).
  280. structure one =
  281.   struct
  282.     fun a/1.
  283.   end.
  284.  
  285. structure two =
  286.   struct
  287.     fun b/1 = one:b.
  288.   end.
  289.  
  290. % ---------------------------------------------------------------------------
  291.  
  292. % Testing bad functions (III).
  293.  
  294. % $consult(user,[v,s]).
  295. structure one =
  296.   struct
  297.     fun a/1.
  298.   end.
  299.  
  300. structure two =
  301.   struct
  302.     fun b/1 = three:a.
  303.   end.
  304.  
  305. % ---------------------------------------------------------------------------
  306.  
  307. % Testing okay function use.
  308.  
  309. % $consult(user,[v,s]).
  310. structure one =
  311.   struct
  312.     fun a/1.
  313.   end.
  314.  
  315. structure two =
  316.   struct
  317.     fun b/1 = one:a.
  318.   end.
  319.  
  320. % ---------------------------------------------------------------------------
  321.  
  322. % Testing nested structures.
  323.  
  324. % $consult(user,[v,s]).
  325. structure outside =
  326.   struct
  327.     structure middle =
  328.       struct
  329.         predicate(_).
  330.         structure inside =
  331.           struct
  332.             predicate(_).
  333.           end.
  334.       end.
  335.     predicate(_).
  336.   end.
  337.  
  338. % Examples
  339.  
  340. % outside:predicate(X).
  341. % inside:predicate(X).
  342. % outside:middle:inside:predicate(X).
  343.  
  344. % ---------------------------------------------------------------------------
  345.  
  346. % Testing how command-line responds to multiple solutions to call
  347.  
  348. % $consult(user,[v,s]).
  349. signature match =
  350.   sig
  351.     pred pred1/1.
  352.     fun fun1/0.
  353.   end.
  354.  
  355. structure test/match =
  356.   struct
  357.     fun fun1/0.
  358.     pred1(fun1).
  359.     pred1(fun1).
  360.     pred1(fun1).
  361.     pred1(fun1).
  362.     pred1(fun1).
  363.     pred1(fun1).
  364.   end.
  365.  
  366. % Examples
  367.  
  368. % test:pred1(X).
  369.  
  370. % ---------------------------------------------------------------------------
  371.  
  372. % Test to see how command-line responds to more than one function with same
  373. % internal form.
  374.  
  375. % $consult(user,[v,s]).
  376. structure test = 
  377.   struct
  378.     fun aa/0.
  379.     fun bb/0 = aa.
  380.     pp(aa).
  381.   end.
  382.  
  383. % Examples
  384.  
  385. % test:pp(X).
  386.  
  387. % ---------------------------------------------------------------------------
  388.  
  389. % Signature should not have a type (I).
  390.  
  391. % $consult(user,[v,s]).
  392. signature yah/yah =
  393.   sig
  394.     pred test/0.
  395.   end.
  396.  
  397. % ---------------------------------------------------------------------------
  398.  
  399. % Signature should not have a type (II).
  400.  
  401. % $consult(user,[v,s]).
  402. signature yah =
  403.   sig
  404.     pred test/0.
  405.   end/yah.
  406.  
  407. % ---------------------------------------------------------------------------
  408.  
  409. % Functor signature mismatch.
  410.  
  411. % $consult(user,[v,s]).
  412. functor a(a/a,b/b)/type =
  413.   struct
  414.     test.
  415.   end/anothertype.
  416.  
  417. % ---------------------------------------------------------------------------
  418.  
  419. % Functor with variable signature.
  420.  
  421. % $consult(user,[v,s]).
  422. functor a(a/a,b/b)/Var =
  423.   struct
  424.     test.
  425.   end.
  426.  
  427. % ---------------------------------------------------------------------------
  428.  
  429. % Structure signature mismatch.
  430.  
  431. % $consult(user,[v,s]).
  432. structure yah/type =
  433.   struct
  434.     test.
  435.   end/anothertype.
  436.  
  437. % ---------------------------------------------------------------------------
  438.  
  439. % Structure with variable signature.
  440.  
  441. % $consult(user,[v,s]).
  442. structure yah/Var =
  443.   struct
  444.     test.
  445.   end.
  446.  
  447. % ---------------------------------------------------------------------------
  448.  
  449. % Signature with pervasive pred/fun and duplicate
  450. % declarations.
  451.  
  452. % $consult(user,[v,s]).
  453. signature test =
  454.   sig
  455.     pred append/2 and assert/1.
  456.     fun a/0.
  457.     pred test/1 and test/1.
  458.     fun test/1 and test/1.
  459.   end.
  460.  
  461. % ---------------------------------------------------------------------------
  462.  
  463. % Structure with duplicate function declarations and
  464. % pervasive function declarations.
  465.  
  466. % $consult(user,[v,s]).
  467. structure test =
  468.   struct
  469.     fun a/1 and a/1.
  470.     fun a/0.
  471.     fun a/1 = a.
  472.   end.
  473.  
  474. % ---------------------------------------------------------------------------
  475.  
  476. % Testing sharing.
  477.  
  478. % $consult(user,[v,s]).
  479. signature btreedatasig1 =
  480.   sig
  481.     pred isleaf/1 and isnode/1 and mkleaf/1 and mknode/4 and label/2 and
  482.            leftchild/2 and rightchild/2.
  483.   end.
  484.  
  485. signature btreemem =
  486.   sig
  487.     structure b/btreedatasig1.      pred newmember/2.
  488.   end.
  489.  
  490. signature btreeeq =
  491.   sig
  492.     structure c/btreedatasig1.      pred eqtree/2.
  493.   end.
  494.  
  495. structure btreedata1/btreedatasig1 =
  496.   struct
  497.     fun leaf/0 and tree/3.          isleaf(leaf).
  498.     isnode(tree(_,_,_)).            mkleaf(leaf).
  499.     mknode(A,L,R,tree(A,L,R)).      label(tree(A,_,_), A).
  500.     leftchild(tree(_,L,_), L).      rightchild(tree(_,_,R), R).
  501.   end.
  502.  
  503. structure btreedata2/btreedatasig1 =
  504.   struct
  505.     fun leaf/0 and tree/3.          isleaf(leaf).
  506.     isnode(tree(_,_,_)).            mkleaf(leaf).
  507.     mknode(A,L,R,tree(A,L,R)).      label(tree(A,_,_), A).
  508.     leftchild(tree(_,L,_), L).      rightchild(tree(_,_,R), R).
  509.   end.
  510.  
  511. functor absbtreeeq(x/btreedatasig1) =
  512.   struct
  513.     structure c = x.
  514.     eqtree(Tree1, Tree2) :- c:isleaf(Tree1), c:isleaf(Tree2).
  515.     eqtree(Tree1, Tree2) :- c:label(Tree1, Label), c:label(Tree2, Label),
  516.           c:leftchild(Tree1, Left1), c:leftchild(Tree2, Left2),
  517.           c:rightchild(Tree1, Right1),c:rightchild(Tree2, Right2),
  518.           eqtree(Left1, Left2), eqtree(Right1, Right2).
  519.   end.
  520.  
  521. functor absbtreemem(x/btreedatasig1) =
  522.   struct
  523.     structure b = x.
  524.     newmember(A, Tree) :- b:label(Tree, A).
  525.     newmember(A, Tree) :- b:leftchild(Tree, Left), newmember(A, Left).
  526.     newmember(A, Tree) :- b:rightchild(Tree, Right), newmember(A, Right).
  527.   end.
  528.  
  529. functor absbtreeutil(x/btreemem, y/btreeeq sharing x:b=y:c) =
  530.   struct
  531.     structure u = x.    structure v = y.
  532.     foobar(Elem, Tree1, Tree2) :- u:newmember(Elem, Tree1), 
  533.           v:eqtree(Tree1, Tree2).
  534.   end.
  535.  
  536. structure btreemem = absbtreemem(btreedata1).
  537. structure btreeeq = absbtreeeq(btreedata2).
  538. structure btreeutil = absbtreeutil(btreemem,btreeeq).
  539.  
  540. % Examples
  541.  
  542. % btreeutil:foobar(X,Y,Z).
  543. % btreemem:newmember(X,Y).
  544. % btreeeq:eqtree(X,Y).
  545.  
  546. % ---------------------------------------------------------------------------
  547.  
  548. % Error check - Bad sharing.
  549.  
  550. % $consult(user,[v,s]).
  551. signature btreedatasig1 = sig
  552.                             pred isleaf/1 and isnode/1 and mkleaf/1 and mknode/4
  553.                                  and label/2 and leftchild/2 and rightchild/2.
  554.                           end.
  555.  
  556. signature btreedatasig2 = sig
  557.                             pred isleaf/1 and mknode/4
  558.                                  and label/2 and leftchild/2 and rightchild/2.
  559.                           end.
  560.  
  561. signature btreemem = sig
  562.                        structure b/btreedatasig1.      pred newmember/2.
  563.                      end.
  564.  
  565. signature btreeeq = sig
  566.                       structure c/btreedatasig2.      pred eqtree/2.
  567.                     end.
  568.  
  569. structure btreedata1/btreedatasig1 =
  570.   struct
  571.     fun leaf/0 and tree/3.          isleaf(leaf).
  572.     isnode(tree(_,_,_)).            mkleaf(leaf).
  573.     mknode(A,L,R,tree(A,L,R)).      label(tree(A,_,_), A).
  574.     leftchild(tree(_,L,_), L).      rightchild(tree(_,_,R), R).
  575.   end.
  576.  
  577. structure btreedata2/btreedatasig2 =
  578.   struct
  579.     fun leaf/0 and tree/3.          isleaf(leaf).
  580.     mknode(A,L,R,tree(A,L,R)).      label(tree(A,_,_), A).
  581.     leftchild(tree(_,L,_), L).      rightchild(tree(_,_,R), R).
  582.   end.
  583.  
  584. functor absbtreeeq(x/btreedatasig2) =
  585.   struct
  586.     structure c = x.
  587.     eqtree(Tree1, Tree2) :- c:isleaf(Tree1), c:isleaf(Tree2).
  588.     eqtree(Tree1, Tree2) :- c:label(Tree1, Label), c:label(Tree2, Label),
  589.           c:leftchild(Tree1, Left1), c:leftchild(Tree2, Left2),
  590.           c:rightchild(Tree1, Right1),c:rightchild(Tree2, Right2),
  591.           eqtree(Left1, Left2), eqtree(Right1, Right2).
  592.   end.
  593.  
  594. functor absbtreemem(x/btreedatasig1) =
  595.   struct
  596.     structure b = x.
  597.     newmember(A, Tree) :- b:label(Tree, A).
  598.     newmember(A, Tree) :- b:leftchild(Tree, Left), newmember(A, Left).
  599.     newmember(A, Tree) :- b:rightchild(Tree, Right), newmember(A, Right).
  600.   end.
  601.  
  602. functor absbtreeutil(x/btreemem, y/btreeeq sharing x:b=y:c) =
  603.   struct
  604.     structure u = x.    structure v = y.
  605.     foobar(Elem, Tree1, Tree2) :- u:newmember(Elem, Tree1), 
  606.           v:eqtree(Tree1, Tree2).
  607.   end.
  608.  
  609. structure btreemem = absbtreemem(btreedata1).
  610. structure btreeeq = absbtreeeq(btreedata2).
  611. structure btreeutil = absbtreeutil(btreemem,btreeeq).
  612.